package com.example.geckohurricanes;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class ItemDatabaseHandler extends SQLiteOpenHelper {

	private static final int DATABASE_VERSION = 1;
	private static final String DATABASE_NAME = "itemsManager";
	private static final String TABLE_ITEMS = "items";
	 
	private static final String KEY_ID = "id";
	private static final String KEY_NAME = "name";
	private static final String KEY_DESC = "desc";
	
	public ItemDatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_ITEMS + "("
	               + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
	               + KEY_DESC + " TEXT" + ")";
		db.execSQL(CREATE_CONTACTS_TABLE);
	}
	
	@Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_ITEMS);
        onCreate(db);
    }
	
	//CRUD Operations
	public void addItem(Item item) {
	    SQLiteDatabase db = this.getWritableDatabase();
	 
	    ContentValues values = new ContentValues();
	    values.put(KEY_NAME, item.getName());
	    values.put(KEY_DESC, item.getDesc());
	 
	    db.insert(TABLE_ITEMS, null, values);
	    db.close();
	}
	
	public Item getItem(int id) {
	    SQLiteDatabase db = this.getReadableDatabase();
	 
	    Cursor cursor = db.query(TABLE_ITEMS, new String[] { KEY_ID,
	            KEY_NAME, KEY_DESC }, KEY_ID + "=?",
	            new String[] { String.valueOf(id) }, null, null, null, null);
	    if (cursor != null)
	        cursor.moveToFirst();
	 
	    Item item = new Item(Integer.parseInt(cursor.getString(0)),
	            cursor.getString(1), cursor.getString(2), cursor.getString(2), 1);
	    // return contact
	    return item;
	}
	
	//Gets all contacts
	public List<Item> getAllItems() {
	    List<Item> itemList = new ArrayList<Item>();
	    String selectQuery = "SELECT  * FROM " + TABLE_ITEMS;
	 
	    SQLiteDatabase db = this.getWritableDatabase();
	    Cursor cursor = db.rawQuery(selectQuery, null);
	 
	    if (cursor.moveToFirst()) {
	        do {
	            Item item = new Item();
	            item.setId(Integer.parseInt(cursor.getString(0)));
	            item.setName(cursor.getString(1));
	            item.setDesc(cursor.getString(2));
	            itemList.add(item);
	        } while (cursor.moveToNext());
	    }
	    return itemList;
	}
	
	//Returns total number of contacts
	public int getContactsCount() {
        String countQuery = "SELECT  * FROM " + TABLE_ITEMS;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();
 
        return cursor.getCount();
    }
	
	public int updateContact(Contact contact) {
	    SQLiteDatabase db = this.getWritableDatabase();
	 
	    ContentValues values = new ContentValues();
	    values.put(KEY_NAME, contact.getName());
	    values.put(KEY_DESC, contact.getPass());
	 
	    // updating row
	    return db.update(TABLE_ITEMS, values, KEY_ID + " = ?",
	            new String[] { String.valueOf(contact.getId()) });
	}
	
	public void deleteItem(Item item) {
	    SQLiteDatabase db = this.getWritableDatabase();
	    db.delete(TABLE_ITEMS, KEY_ID + " = ?",
	            new String[] { String.valueOf(item.getId()) });
	    db.close();
	}
}
